package com.imust.test;

import com.imust.test.system.Guest;

import java.awt.event.*;
import java.sql.SQLException;

import javax.swing.*;
// QQ登录监听器
public class Check implements ActionListener {
    private JComboBox<Object> jco; // 用来获取QQ账号的对象
    private JPasswordField jpa; // 用来获取QQ密码的对象
    private JFrame frame;
    public Check(JComboBox<Object> jco, JPasswordField jpa, JFrame frame) {
        super();
        this.jco = jco;
        this.jpa = jpa;
        this.frame = frame;
    }
    /**
     * 为登录动作监听事件执行处理
     */
    public void actionPerformed(ActionEvent e) {
        // 1、获取登录的账号和密码
        String a = (String) jco.getSelectedItem();
        String b = new String(jpa.getPassword());
        // 创建LoginDao对象
        Read connectDB = new Read();
        // 查询登录用户，如果有此用户并且密码正确则返回true
        Boolean bl =false;
        try {
            bl = connectDB.findUser(a, b);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        // 2、判断输入的账号和密码是否正确
        if (bl) {
            // 账号正确，先关闭当前JFrame登录窗口
            frame.dispose();
            JOptionPane.showMessageDialog(null, "登录成功！");
            // 模拟显示登录成功后的QQ窗口
            Guest.main();
        } else {
            // QQ账号或密码输入错误，弹出提示信息
            JOptionPane.showMessageDialog(null, "你输入的账户名或密码不正确，请重新输入！");
        }
    }
}